package plugins.test;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.form.gpt.IGPTAction;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.user.UserServiceHelper;
import kd.bos.context.RequestContext;

import java.util.HashMap;
import java.util.Map;

public class newone implements IGPTAction {
    @Override
    public Map<String, String> invokeAction(String action, Map<String, String> params) {
        Map<String, String> result = new HashMap<>();
        if ("GET_MESSAGE".equalsIgnoreCase(action)) {
            String userName = RequestContext.get().getUserName();

            // 查询用户健康数据
            DynamicObject[] dys = BusinessDataServiceHelper.load(
                    "dcb4_userdata",
                    "dcb4_height,dcb4_weight,dcb4_bmi",
                    new QFilter[]{new QFilter("name", QCP.equals, userName)}
            );

            JSONArray jsonArray = new JSONArray();
            for (DynamicObject dynamicObject : dys) {
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("height", dynamicObject.get("dcb4_height"));
                jsonObject.put("weight", dynamicObject.get("dcb4_weight"));
                jsonObject.put("bmi", dynamicObject.get("dcb4_bmi"));
                jsonArray.add(jsonObject);
            }

            result.put("resultDynamicObject", jsonArray.toJSONString());
        }
        return result;


    }
}
